<template>
	<view class="news">
		<view class="clinics" v-for="(item,index) in list" :key="list.item" @click="jumpChat(index)">
			<view class="images">
				<image :src="item.userProfile.avatar" mode="scaleToFill" style="width: 100rpx; height: 100rpx;"></image>
			</view>
			<view class="nickLastTime">
				<view class="nick">
					<text>{{item.userProfile.nick}}</text>
					<text>{{item.lastMessage.lastTime | formdate()}}</text>
				</view>
				<view class="lastTime">
					<text>{{item.lastMessage.messageForShow}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import TIM from 'tim-wx-sdk';
	export default {
		onShow() {
			// 当用户没有登录时跳转登录
			this.jumpMy();
			// 得到用户信息
			this.getMyInfo();
			// 获取消息列表
			this.getMsgList();
		},
		data() {
			return {
				list: null,
				userIDList: [],
			};
		},
		methods: {
			// 当用户没有填写信息时跳转页面了吧
			tiaochu() {
				let tiems = 2;
				uni.showToast({
					title: "请填写我信息",
					icon: "none",
					duration: tiems * 1000,
					success() {
						setTimeout(() => {
							uni.switchTab({
								url: "../my/my"
							});
						}, tiems * 1000);
					}
				});
			},
			// 得到我的信息，
			getMyInfo() {
				try {
					let userAge = this.$store.state.userinfo.user_age;
					let userName = this.$store.state.userinfo.user_name;
					let userSex = this.$store.state.userinfo.user_sex;
					if (userAge == "null" || userAge == "" || userAge === null) {
						this.tiaochu();
					};
					if (userName == "null" || userName == "" || userName === null) {
						this.tiaochu();
					};
					if (userSex == "null" || userSex == "" || userSex === null) {
						this.tiaochu();
					};
				} catch (err) {
					console.error(err);
				};
			},
			// 拉取会话列表
			getMsgList() {
				let self = this;
				this.globalData.tim.getConversationList().then(res => {
					self.list = res.data.conversationList;
				}).catch(err => {
					console.error(err);
				});
			},

			// 没有openid时成功提示信息后，并跳转到登录页面
			jumpMy() {
				try {
					if (!this.$store.state.userinfo) {
						let times = 2;
						uni.showToast({
							title: "请求点击登录",
							duration: times * 1000,
							icon: "none",
							success: (res) => {
								let setId = setTimeout(() => {
									uni.switchTab({
										url: "../my/my"
									})
									clearTimeout(setId);
								}, times * 1000);
							}
						})
						return false;
					};
				} catch (err) {
					console.error(err);
				};
			},

			jumpChat(index) {
				let doctInfo = this.list.filter((value, key, arr) => {
					if (index == key) {
						return value;
					};
				});
				uni.navigateTo({
					url: `../../chat/chat?openid=${doctInfo[0].userProfile.userID}`
				})
			},
		},

		"filters": {
			formdate(value) {
				let date = new Date(value * 1000);
				let h = date.getHours() + ':';
				let m = date.getMinutes();
				return h + m;
			}
		}
	}
</script>

<style lang="scss">
	.news {
		width: 750rpx;
		height: 100vh;
		background-color: #F5F8FA;
		overflow: hidden;
	}

	.clinics {
		display: flex;
		width: 690rpx;
		height: 140rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		margin: 20rpx auto 0rpx;
		padding: 20rpx;
	}

	.images {
		margin-right: 25rpx;

		image {
			border-radius: 50%;
		}
	}

	.nickLastTime {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
	}

	.nick,
	.lastTime {
		@include chuizhilianduan;
		width: 100%;
	}

	.nick {
		>text {
			font-family: PingFang SC;

			&:first-of-type {
				font-size: 32rpx;
				font-weight: 400;
				color: rgba(40, 40, 40, 1);
			}

			&:last-of-type {
				font-size: 22rpx;
				color: rgba(138, 138, 153, 1);
			}
		}
	}

	.lastTime {
		>text {
			&:first-of-type {
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: rgba(138, 138, 153, 1);
			}
		}
	}
</style>
